package com.nami.ucenter.controller.api;

import cn.hutool.core.util.RandomUtil;
import com.nami.ucenter.controller.convertor.UserConvert;
import com.nami.ucenter.facade.login.UserLoginFacade;
import com.nami.ucenter.facade.login.response.UserLogin;
import com.nami.ucenter.domain.model.UserModel;
import com.nami.ucenter.domain.port.repository.UserRepository;
import com.nami.ucenter.domain.service.UserLoginDomainService;
import com.nami.ucenter.controller.dto.UserAddDTO;
import com.nami.ucenter.facade.login.request.query.UserLoginQry;
import com.nami.ucenter.controller.utils.ChineseNameUtil;
import com.nami.ucenter.controller.utils.RandomUtils;
import com.nami.ucenter.controller.vo.UserInfoVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.exception.ServiceException;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * APP - 用户
 *
 * @author lbc
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/user")
public class UserController {

    private final UserRepository userRepository;

    private final UserLoginDomainService userDomainService;
    private final UserLoginFacade loginFacade;

    /**
     * @param userId
     * @return
     */
    @GetMapping("/info")
    public R<UserInfoVO> getInfo(String userId) {
        UserModel user = userRepository.getUserByUserId(userId);
        return R.ok(toVO(userId, user));
    }

    private static UserInfoVO toVO(String userId, UserModel user) {
        UserInfoVO result = new UserInfoVO();
        if (user == null) {
            throw new ServiceException("用户不存在！");
        }
        result.setUserId(userId);
        result.setAvatar(user.getAvatarUrl());
        result.setNick(user.getNickname());
        result.setMobile(user.getMobile());
        return result;
    }

    @PostMapping("/login")
    public R<UserLogin> login(@Validated @RequestBody UserLoginQry request) {
        return R.ok(loginFacade.execute(request));
    }

    @PostMapping("/register")
    public R<Boolean> register(@Validated @RequestBody UserAddDTO dto) {
        return R.ok(userDomainService.register(toModel(dto)));
    }

    private UserModel toModel(UserAddDTO dto) {
        UserModel request = new UserModel();
        request.setMobile(dto.getMobile());
        request.setAccount(dto.getMobile());
        request.setPassword(dto.getPassword());
        request.setGender(dto.getGender());
        request.setEmail(dto.getEmail());
        request.setAppKey(dto.getAppKey());
        request.setAvatarUrl(dto.getAvatar());
        request.setNickname(dto.getNickname());
        request.setRealName(dto.getNickname());
        return request;
    }


    /**
     * 随机生成用户
     *
     * @param
     * @return
     */
    @GetMapping("/genRandomUser")
    public R<Boolean> randomINewUsers() {

        List<UserModel> requests = new ArrayList<>();
//        for (int i = 7; i < 3000; i++) {
//            // register
//            UserAddRequest userInfo = new UserAddRequest();
//            userInfo.setUserId(i + "");
//            userInfo.setNickname(ChineseNameUtil.randomName());
//            userInfo.setAvatarUrl("https://file.lwoowl.cn/avatar_jgd/55e321a8-645a-47ae-99ae-284a70086b95.png");
//            userInfo.setAppKey("test");
//            userInfo.setUserType(10);
//            userInfo.setMobile(RandomUtil.randomMobile());
//            userInfo.setStatus(10);
//            userInfo.setUserType(10);
//            requests.add(userInfo);
//        }
        for (int i = 10; i < 3000; i++) {
            // register
            UserModel userInfo = new UserModel();
            userInfo.setUserId(i + "");
            userInfo.setNickname(ChineseNameUtil.randomName());
            userInfo.setRealName(userInfo.getNickname());
            userInfo.setAvatarUrl("https://file.lwoowl.cn/avatar_jgd/55e321a8-645a-47ae-99ae-284a70086b95.png");
            userInfo.setAppKey("test");
            userInfo.setUserType(10);
            userInfo.setMobile(RandomUtils.randomMobile());
            userInfo.setAccount(userInfo.getMobile());
            userInfo.setStatus(10);
            userInfo.setUserType(10);

            userInfo.setGender(1);
            if (i / 2 == 0) {
                userInfo.setGender(2);
            }

            userInfo.setSalt(RandomUtil.randomString(6));
            userInfo.setGmtCreate(new Date());
            userInfo.setGmtModified(new Date());
            requests.add(userInfo);
        }

        userRepository.addALl(requests);
        return R.ok(Boolean.TRUE);
    }


}
